package no.ntnu.fp.mmi;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import no.ntnu.fp.su.WatchWell;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EditWellPanel extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JTextField rValue;
	private JTextField pValue;
	private JTextField deltaP2limit;
	private JTextField deltaP1limit;
	private JTextField deltaPlimit;
	private JTextField deltaTlimit;
	private JTextField evLimit;
	private JTextField tValue;
	private JLabel rName;
	private JSeparator separator0;
	private JButton cancelButton;
	private JButton saveButton;
	private JSeparator seperator2;
	private JLabel P2name;
	private JLabel P1name;
	private JLabel Pname;
	private JLabel Tname;
	private JLabel EVname;
	private JLabel LIMname;
	private JLabel cValuesName;
	private JLabel tName;
	private JLabel pName;
	private JSeparator seperator;
	private JLabel editWell;
	Font bold = new Font("Candera", Font.BOLD, 12);
	private WatchWell watchWell;
	private WatchPanel panel;

	/**
	 *  Constructor for editwellpanel <br>
	 *  sets layout for panel
	 
	 */
	public EditWellPanel(WatchWell well, WatchPanel panel) {
		super();
		this.watchWell = well;
		this.panel = panel;
		

		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			setSize(350, 350);
				
			{
				Font font = new Font("Candera", Font.BOLD, 16);
				editWell = new JLabel();
				getContentPane().add(editWell);
				editWell.setText("Edit: " + well.getName());
				editWell.setBounds(27, 12, 143, 15);
				editWell.setFont(font);
				
			}
			{
				rValue = new JTextField();
				rValue.setText(""+well.getCritR());
				getContentPane().add(rValue);
				rValue.setBounds(82, 63, 70, 25);

			}
			{
				pValue = new JTextField();
				pValue.setText(""+well.getCritP());
				getContentPane().add(pValue); 
				pValue.setBounds(82, 91, 70, 25);
				

			}
			{
				tValue = new JTextField();
				tValue.setText(""+well.getCritT());
				getContentPane().add(tValue);
				tValue.setBounds(82, 120, 70, 25);
			}
			{
				seperator = new JSeparator();
				getContentPane().add(seperator);
				seperator.setBounds(32, 154, 261, 8);
			}
			{
				rName = new JLabel();
				getContentPane().add(rName);
				rName.setText("R");
				rName.setBounds(65, 65, 10, 15);
			}
			{
				pName = new JLabel();
				getContentPane().add(pName);
				pName.setText("P");
				pName.setBounds(65, 94, 10, 15);
			}
			{
				tName = new JLabel();
				getContentPane().add(tName);
				tName.setText("T");
				tName.setBounds(65, 123, 10, 15);
			}
			{
				cValuesName = new JLabel();
				getContentPane().add(cValuesName);
				cValuesName.setText("Critical Values");
				cValuesName.setBounds(43, 46, 90, 15);
				cValuesName.setFont(bold);
			}
			{
				LIMname = new JLabel();
				getContentPane().add(LIMname);
				LIMname.setText("Limits");
				LIMname.setBounds(38, 168, 50, 15);
				LIMname.setFont(bold);
			}
			{
				evLimit = new JTextField();
				evLimit.setText(""+well.getEvLim());
				getContentPane().add(evLimit);
				evLimit.setBounds(80, 183, 70, 25);
			}
			{
				deltaTlimit = new JTextField();
				deltaTlimit.setText(""+well.getDeltaTlim());
				getContentPane().add(deltaTlimit);
				deltaTlimit.setBounds(80, 208, 70, 25);
			}
			{
				deltaPlimit = new JTextField();
				deltaPlimit.setText(""+well.getPLim());
				getContentPane().add(deltaPlimit);
				deltaPlimit.setBounds(80, 235, 70, 25);
			}
			{
				EVname = new JLabel();
				getContentPane().add(EVname);
				EVname.setText("EV ");
				EVname.setBounds(62, 189, 18, 15);
			}
			{
				Tname = new JLabel();
				getContentPane().add(Tname);
				Tname.setText("\u0394T");
				Tname.setBounds(62, 214, 18, 15);
			}
			{
				Pname = new JLabel();
				getContentPane().add(Pname);
				Pname.setText("\u0394P");
				Pname.setBounds(62, 241, 18, 15);
			}
			{
				deltaP1limit = new JTextField();
				deltaP1limit.setText(""+well.getDeltaPlim1());
				getContentPane().add(deltaP1limit);
				deltaP1limit.setBounds(212, 183, 70, 25);
			}
			{
				deltaP2limit = new JTextField();
				deltaP2limit.setText(""+well.getDeltaPlim2());
				getContentPane().add(deltaP2limit);
				deltaP2limit.setBounds(212, 208, 70, 25);
			}
			{
				P1name = new JLabel();
				getContentPane().add(P1name);
				P1name.setText("\u0394P1");
				P1name.setBounds(176, 187, 24, 15);
			}
			{
				P2name = new JLabel();
				getContentPane().add(P2name);
				P2name.setText("\u0394P2");
				P2name.setBounds(176, 214, 24, 15);
			}
			{
				seperator2 = new JSeparator();
				getContentPane().add(seperator2);
				seperator2.setBounds(32, 271, 261, 10);
			}
			{
				saveButton = new JButton();
				getContentPane().add(saveButton);
				saveButton.setText("Save");
				saveButton.setBounds(32, 280, 86, 22);
				saveButton.addActionListener(new SaveButtonEvent());
			}
			{
				cancelButton = new JButton();
				getContentPane().add(cancelButton);
				cancelButton.setText("Cancel");
				cancelButton.setBounds(207, 280, 86, 22);
				cancelButton.addActionListener(new CancelButtonEvent());
			}
			{
				separator0 = new JSeparator();
				getContentPane().add(separator0);
				separator0.setBounds(27, 30, 67, 7);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *  Listener for cancel button, when pressed closes this panel and does not edit a well
	 
	 */
	public class CancelButtonEvent implements ActionListener {

		private Object[] option = {"Yes", "No"};
		public void actionPerformed(ActionEvent e) {
			int val = javax.swing.JOptionPane.showOptionDialog(null, "Are you sure you want to exit without saving?",
	                "Cancel?", javax.swing.JOptionPane.YES_NO_OPTION,
	                javax.swing.JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
	        
	        if(val == 0) {
	            dispose();
	        }
			
		}		
	}
	
	/**
	 *  Listener for save button, when pressed closes editpanel and edits the selected well's parameters
	 
	 */
	public class SaveButtonEvent implements ActionListener {
		public void actionPerformed(ActionEvent e) {
				watchWell.setCritR(Double.parseDouble(rValue.getText()));
				watchWell.setCritP(Double.parseDouble(pValue.getText()));
				watchWell.setCritT(Double.parseDouble(tValue.getText()));
				watchWell.setEvLim(Double.parseDouble(evLimit.getText()));
				watchWell.setDeltaTlim(Double.parseDouble(deltaTlimit.getText()));
				watchWell.setPLim(Double.parseDouble(deltaPlimit.getText()));
				watchWell.setDeltaPlim1(Double.parseDouble(deltaP1limit.getText()));
				watchWell.setDeltaPlim2(Double.parseDouble(deltaP2limit.getText()));
				
				JOptionPane.showMessageDialog(null, "Values was successfully saved");
				panel.drawCrits();
				dispose();
			}
			
				
		}


	
	
	
	
	
	
	
	
	
	
	
}
